package com.das.mechanic_base.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PathMeasure;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3ScreenUtils;


/**
 * 作者：zhanglei on 2019/10/10 13:26
 * <p>
 * 邮箱：916256628@qq.com
 */
public class X3TagImageView extends View implements View.OnTouchListener {
    private  Bitmap mBitmap;
    /**
     * 中心圆的画笔
     */
    private Paint mPaint;
    /**
     * 渐变圆的画笔
     */
    private Paint mPaint1;
    /**
     * 路径的画笔
     */
    private Paint mPaint2;

    private Paint bPaint;
    /**
     * 文字的画笔
     */
    private Paint mPaintText;
    /**
     * 中心圆的半径
     */
    private int mRadius = 10;
    /**
     * 圆渐变的动画
     */
    private ValueAnimator mAnimatorCircle;
    /**
     * 测量路径的类
     */
    private PathMeasure mPathMeasure;

    private Float animatedValue = 0f;

    private int mWidth;
    private int mHeight;

    //判断是展开是回收
    private boolean flag;

    //绘制的内容
    private String text;

    public enum Direction {Left, Right}

    public Direction direction;

    private Context mContext;

    public static int offSetX = 10;
    public static int offSetY = 10;

    private int r10 = 10;
    private int r5 = 5;
    private int y5 = 5;
    private int r3 = 3;
    private int r16 = 16;
    private int d38 = 38;
    private int d24 = 24;
    private int d25 = 25;
    private int d36 = 36;
    private int d48 = 48;
    private int d47 = 47;
    private int d40 = 40;
    private int viewH;
    private int textSize14 = 14;


    public static final String TAG = "ScaleImageView";
    private boolean isCanTouch = false;
    public static final float SCALE_MAX = 5.0f; //最大的缩放比例
    private static final float SCALE_MIN = 1.0f;
    private double oldDist = 0;
    private double moveDist = 0;
    private float downX1 = 0;
    private float downX2 = 0;
    private float downY1 = 0;
    private float downY2 = 0;

    public int totalW = 0;

    public boolean isHindBitmap ;

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public void setText(String text) {
        this.text = text;
    }

    public X3TagImageView(Context context) {
        this(context, null);
    }

    public X3TagImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public X3TagImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;

//        setBackgroundColor(R.color.bg_F64444);

        viewH= (int) (X3ScreenUtils.dipToPx(52, mContext));
        viewH = 16;


        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.WHITE);

        mPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint1.setStyle(Paint.Style.FILL);
        mPaint1.setColor(Color.WHITE);
        mPaint1.setStrokeWidth(2);

        mPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint2.setStyle(Paint.Style.FILL);
        mPaint2.setColor(Color.WHITE);
        mPaint2.setStrokeWidth(2);

        bPaint = new Paint();
        bPaint.setStyle(Paint.Style.FILL);
        bPaint.setColor(Color.parseColor("#30000000"));

        mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintText.setStyle(Paint.Style.FILL);
        mPaintText.setColor(Color.WHITE);



    }

    /**
     * 增加标签 需要传入 内容+显示方向
     */
    public void changeTag(String title, Direction direction) {
        this.text = title;
        this.direction = direction;
        if (direction == Direction.Left) {
            mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.x3_pic_scale);
        } else {
            mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.x3_pic_scale_left);
        }
        invalidate();
    }

    public void refreshTag(){
        invalidate();
    }

    public void refreshTag(float scale){
        refreshTag(scale,false);
    }

    public void refreshTag(float scale,boolean refresh){
        configScale(scale);
        if (refresh){
            invalidate();
        }
    }

    private void configScale(float scale){
        r10 = (int) (10 * scale);
        r5 = (int) (5 * scale);
        r3 = (int) (3 * scale);
        r16 = (int) (16 * scale);
        d24 = (int) (24 * scale);
        d25 = (int) (25 * scale);
        if (scale > 1){
            d36 = (int) (36 * (scale-1) / 1.5 ) + 36;
        }
        if (scale > 1){
            d47 = (int) (47 * (scale-1) / 2) + 47;
        }
        if (scale > 1){
            d40 = (int) (40 * (scale-1) / 2) + 40;
        }
        if (scale > 1){
            d48 = (int) (48 * (scale-1) / 2) + 48;
        }
        if (scale > 1){
            d38 = (int) (38 * (scale-1) / 2) + 38;
        }
        if (scale > 1){
            y5 = (int) (5 * (scale-1) / 1.5) + 5;
        }
        textSize14 = (int) (14 * scale);

        viewH= (int) (X3ScreenUtils.dipToPx(52, mContext) * scale);
        viewH = viewH / 2 ;
        viewH =  (int) (16 * scale);
    }

    public String getShare() {
        return text;
    }

    public Direction getDirection() {
        return direction;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPaint2.setStyle(Paint.Style.FILL);
        if (direction == Direction.Right) {
            Log.e("Direction.",Direction.Right+"");
            Rect rect = new Rect();
            mPaintText.setTextSize(X3ScreenUtils.dipToPx(textSize14, mContext));
            mPaintText.getTextBounds(text, 0, text.length(), rect);
            String brand = android.os.Build.BRAND;
            int h = viewH * 2;
            if ("HUAWEI".equalsIgnoreCase(brand) || "HONOR".equalsIgnoreCase(brand)){
                h = viewH * 3;
            }

            // 画空心圆
            mPaint1.setColor(Color.parseColor("#30000000"));
            canvas.drawCircle(rect.width() + X3ScreenUtils.dipToPx(r10+38,mContext),h, X3ScreenUtils.dipToPx(r10, mContext), mPaint1);
            mPaint1.setColor(Color.WHITE);
            canvas.drawCircle(rect.width() + X3ScreenUtils.dipToPx(r10+38,mContext), h, X3ScreenUtils.dipToPx(r5, mContext), mPaint1);
            mPaint1.setColor(ContextCompat.getColor(mContext, R.color.cc0077ff));
            canvas.drawCircle(rect.width() + X3ScreenUtils.dipToPx(r10+38,mContext), h, X3ScreenUtils.dipToPx(r3, mContext), mPaint1);

            mPaint2.setColor(Color.parseColor("#30000000"));
            canvas.drawRoundRect(X3ScreenUtils.dipToPx(5,mContext),0,rect.width() + X3ScreenUtils.dipToPx(24,mContext),getHeight()- X3ScreenUtils.dipToPx(offSetY,mContext),X3ScreenUtils.dipToPx(16,mContext), X3ScreenUtils.dipToPx(16,mContext),mPaint2);


            mPaint2.setStyle(Paint.Style.STROKE);
            mPaint2.setStrokeWidth(X3ScreenUtils.dipToPx(1,mContext));
            mPaint2.setColor(Color.WHITE);
            canvas.drawRoundRect(X3ScreenUtils.dipToPx(6,mContext),
                    X3ScreenUtils.dipToPx(1,mContext),rect.width() + X3ScreenUtils.dipToPx(23,mContext),
                    getHeight() - X3ScreenUtils.dipToPx(offSetY + 1,mContext),
                    X3ScreenUtils.dipToPx(16,mContext), X3ScreenUtils.dipToPx(16,mContext),mPaint2);

            canvas.drawText(text, X3ScreenUtils.dipToPx(17,mContext), getHeight() - X3ScreenUtils.dipToPx(offSetY,mContext) - rect.height() / 2 - X3ScreenUtils.dipToPx(y5,mContext), mPaintText);

            //缩放按钮
            if (mBitmap != null && isHindBitmap == false){
                Rect src = new Rect(0,0,mBitmap.getWidth(),mBitmap.getHeight());
                Rect dst = new Rect( 0,
                        getHeight() - X3ScreenUtils.dipToPx(25,mContext),
                        X3ScreenUtils.dipToPx(20,mContext),
                        getHeight()  - X3ScreenUtils.dipToPx(5,mContext) );
                canvas.drawBitmap(mBitmap, src,dst,null);
            }

        } else {
            // 画空心圆
            Log.e("Direction.",Direction.Left+"");
            mPaint1.setColor(Color.parseColor("#30000000"));
            canvas.drawCircle(X3ScreenUtils.dipToPx(r10, mContext), X3ScreenUtils.dipToPx(viewH, mContext), X3ScreenUtils.dipToPx(r10, mContext), mPaint1);
            mPaint1.setColor(Color.WHITE);
            canvas.drawCircle(X3ScreenUtils.dipToPx(r10, mContext), X3ScreenUtils.dipToPx(viewH, mContext), X3ScreenUtils.dipToPx(r5, mContext), mPaint1);
            mPaint1.setColor(ContextCompat.getColor(mContext, R.color.cc0077ff));
            canvas.drawCircle(X3ScreenUtils.dipToPx(r10, mContext), X3ScreenUtils.dipToPx(viewH, mContext), X3ScreenUtils.dipToPx(r3, mContext), mPaint1);

            Rect rect = new Rect();
            mPaintText.setTextSize(X3ScreenUtils.dipToPx(textSize14, mContext));
            mPaintText.getTextBounds(text, 0, text.length(), rect);

            mPaint2.setColor(Color.parseColor("#30000000"));
            canvas.drawRoundRect(X3ScreenUtils.dipToPx(d24,mContext),
                    0,rect.width() + X3ScreenUtils.dipToPx(d48,mContext),getHeight() - X3ScreenUtils.dipToPx(offSetY,mContext),
                    X3ScreenUtils.dipToPx(16,mContext), X3ScreenUtils.dipToPx(16,mContext),mPaint2);

            mPaint2.setStyle(Paint.Style.STROKE);
            mPaint2.setStrokeWidth(X3ScreenUtils.dipToPx(1,mContext));
            mPaint2.setColor(Color.WHITE);
            canvas.drawRoundRect(X3ScreenUtils.dipToPx(d25,mContext),
                    X3ScreenUtils.dipToPx(1,mContext),rect.width() + X3ScreenUtils.dipToPx(d47,mContext),
                    getHeight() - X3ScreenUtils.dipToPx(offSetY,mContext) - X3ScreenUtils.dipToPx(1,mContext),
                    X3ScreenUtils.dipToPx(16,mContext), X3ScreenUtils.dipToPx(16,mContext),mPaint2);

            canvas.drawText(text, X3ScreenUtils.dipToPx(d36,mContext), getHeight() - X3ScreenUtils.dipToPx(offSetY,mContext) - rect.height() / 2 - X3ScreenUtils.dipToPx(y5,mContext), mPaintText);

            //缩放按钮
            if (mBitmap != null && isHindBitmap == false){
                Rect src = new Rect(0,0,mBitmap.getWidth(),mBitmap.getHeight());
                Rect dst = new Rect( rect.width() + X3ScreenUtils.dipToPx(d40,mContext)  ,getHeight() - X3ScreenUtils.dipToPx(30,mContext),
                        rect.width() + X3ScreenUtils.dipToPx(d40,mContext) + X3ScreenUtils.dipToPx(20,mContext)  ,getHeight()  - X3ScreenUtils.dipToPx(10,mContext));
                canvas.drawBitmap(mBitmap, src,dst,null);
            }
        }
    }


    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mAnimatorCircle != null) {
            mAnimatorCircle.cancel();
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.d(TAG, "onTouch isCanTouch= " + isCanTouch);
        int pointerCount = event.getPointerCount();

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_UP:
                if (pointerCount == 2) {
                    downX1 = 0;
                    downY1 = 0;
                    downX2 = 0;
                    downY2 = 0;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (pointerCount == 2) {

                    float x1 = event.getX(0);
                    float x2 = event.getX(1);
                    float y1 = event.getY(0);
                    float y2 = event.getY(1);

                    double changeX1 = x1 - downX1;
                    double changeX2 = x2 - downX2;
                    double changeY1 = y1 - downY1;
                    double changeY2 = y2 - downY2;

                    if (getScaleX() > 1) { //滑动
                        float lessX = (float) ((changeX1) / 2 + (changeX2) / 2);
                        float lessY = (float) ((changeY1) / 2 + (changeY2) / 2);
                        setSelfPivot(-lessX, -lessY);
                    }
                    //缩放处理
                    moveDist = spacing(event);
                    double space = moveDist - oldDist;
                    float scale = (float) (getScaleX() + space / v.getWidth());
                    if (scale < SCALE_MIN) {
                        setScale(SCALE_MIN);
                    } else if (scale > SCALE_MAX) {
                        setScale(SCALE_MAX);
                    } else {
                        setScale(scale);
                    }
                }
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                if (pointerCount == 2) {
                    downX1 = event.getX(0);
                    downX2 = event.getX(1);
                    downY1 = event.getY(0);
                    downY2 = event.getY(1);
                    oldDist = spacing(event); //两点按下时的距离
                }
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
            default:
                break;
        }
        return true;
    }

    /**
     * 触摸使用的移动事件
     *
     * @param lessX
     * @param lessY
     */
    private void setSelfPivot(float lessX, float lessY) {
        float setPivotX = 0;
        float setPivotY = 0;
        setPivotX = getPivotX() + lessX;
        setPivotY = getPivotY() + lessY;
        if (setPivotX < 0 && setPivotY < 0) {
            setPivotX = 0;
            setPivotY = 0;
        } else if (setPivotX > 0 && setPivotY < 0) {
            setPivotY = 0;
            if (setPivotX > getWidth()) {
                setPivotX = getWidth();
            }
        } else if (setPivotX < 0 && setPivotY > 0) {
            setPivotX = 0;
            if (setPivotY > getHeight()) {
                setPivotY = getHeight();
            }
        } else {
            if (setPivotX > getWidth()) {
                setPivotX = getWidth();
            }
            if (setPivotY > getHeight()) {
                setPivotY = getHeight();
            }
        }
        setPivot(setPivotX, setPivotY);
    }

    /**
     * 计算两个点的距离
     *
     * @param event
     * @return
     */
    private double spacing(MotionEvent event) {
        if (event.getPointerCount() == 2) {
            float x = event.getX(0) - event.getX(1);
            float y = event.getY(0) - event.getY(1);
            return Math.sqrt(x * x + y * y);
        } else {
            return 0;
        }
    }

    /**
     * 平移画面，当画面的宽或高大于屏幕宽高时，调用此方法进行平移
     *
     * @param x
     * @param y
     */
    public void setPivot(float x, float y) {
        setPivotX(x);
        setPivotY(y);
    }


    /**
     * 设置放大缩小
     *
     * @param scale
     */
    public void setScale(float scale) {
        setScaleX(scale);
        setScaleY(scale);
    }

    /**
     * 初始化比例，也就是原始比例
     */
    public void setInitScale() {
        setScaleX(1.0f);
        setScaleY(1.0f);
        setPivot(getWidth() / 2, getHeight() / 2);
    }

    public boolean isCanTouch() {
        return isCanTouch;
    }

    public void setCanTouch(boolean canTouch) {
        isCanTouch = canTouch;
    }

    InnerMoveEvent innerMoveEvent;
    public void setInnerMoveEvent(InnerMoveEvent innerMoveEvent){
        this.innerMoveEvent = innerMoveEvent;
    }
    public interface InnerMoveEvent{
        public void onInnerMoveEVent( MotionEvent event);
    }

    public boolean isHindBitmap() {
        return isHindBitmap;
    }

    public void setHindBitmap(boolean hindBitmap) {
        isHindBitmap = hindBitmap;
    }
}
